package com.tianbing.adlottery.controller.user;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.validation.BindException;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.SimpleFormController;

import com.fengniu.ejb.business.IUserBusiness;
import com.fengniu.ejb.dao.IContentDao;
import com.fengniu.ejb.dao.ISiteDao;
import com.fengniu.ejb.dao.IUserDao;
import com.fengniu.ejb.entity.DeltaRelation;
import com.fengniu.ejb.entity.User2Content;
import com.tianbing.adlottery.helper.SkinUtils;
import com.tianbing.adlottery.util.ControllerFormBean;
import com.tianbing.adlottery.util.UserSession;

public class EditMyFloatBookReplyController extends SimpleFormController {
	static Logger logger = Logger.getLogger(EditMyFloatBookReplyController.class.getName());

	private IUserDao userDao;

	private IContentDao contentDao;

	private ISiteDao siteDao;

	private IUserBusiness userBusiness;

	private SkinUtils skinUtils;

	// ------------------GET Request LifeCycle------------------
	protected Object formBackingObject(HttpServletRequest request) throws Exception {
		ControllerFormBean fb = new ControllerFormBean();
		long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		User2Content u2c = userDao.findUser2ContentBy(u2csid);
		if (u2c != null) {
			DeltaRelation deltaRelation = new DeltaRelation();
			deltaRelation.setUser2Content2(u2c);
			fb.setDeltaUCSGRelation(deltaRelation);
		}
		return fb;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#onSubmit(javax.servlet.http.HttpServletRequest,
	 *      javax.servlet.http.HttpServletResponse, java.lang.Object,
	 *      org.springframework.validation.BindException)
	 */
	protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object cmd,
			BindException ex) throws Exception {

		Long u2csid = ServletRequestUtils.getLongParameter(request, "u2csid", -1);
		ModelAndView mv = new ModelAndView(this.getSuccessView() + "?u2csid=" + u2csid);
		ControllerFormBean form = (ControllerFormBean) cmd;
		DeltaRelation delta = form.getDeltaUCSGRelation();
		// String name = ServletRequestUtils.getStringParameter(request, "name",
		// "");
		// String longDesc = ServletRequestUtils.getStringParameter(request,
		// "longDesc", "");
		// delta.setName(name);
		// delta.setLongDesc(longDesc);

		UserSession auth = skinUtils.getUserAuthorization(request, null);
		userBusiness.saveUserFloatBookRepliedMessage(auth.getUserSysId(), delta, u2csid);

		return mv;
	}

	@SuppressWarnings("unchecked")
	protected Map referenceData(HttpServletRequest request) throws Exception {
		Map model = new HashMap();

		return model;
	}

	public void setSkinUtils(SkinUtils skinUtils) {
		this.skinUtils = skinUtils;
	}

	public static void setLogger(Logger logger) {
		EditMyFloatBookReplyController.logger = logger;
	}

	public void setUserDao(IUserDao userDao) {
		this.userDao = userDao;
	}

	public void setUserBusiness(IUserBusiness userBusiness) {
		this.userBusiness = userBusiness;
	}

	public void setSiteDao(ISiteDao siteDao) {
		this.siteDao = siteDao;
	}

	public void setContentDao(IContentDao contentDao) {
		this.contentDao = contentDao;
	}

}
